package pig.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class CustomerDAOImpl implements CustomerDAO {
	List<TransactionVO> list = new ArrayList<TransactionVO>();
	Map<String,Object> map = new HashMap<String,Object>();
	
	@Resource(name="sqlSessionTemplate")
	private SqlSessionTemplate sqlSessionTemplate;

	// 구매자 회원가입
	@Override
	public void customerRegister(CustomerVO customerVO) {
		sqlSessionTemplate.insert("customer.register",customerVO);
	}

	// 아이디 중복체크
	@Override
	public int idCheck(String id) {
		return sqlSessionTemplate.selectOne("customer.idcheck",id);
	}
	
	//장바구니 추가
	@Override
	public void cartAddDeal(CartVO cartVO) {
		sqlSessionTemplate.insert("customer.addCart",cartVO);
	}
	
	// 장바구니 보여주기
	@Override
	public List<CartVO> cartShowDeal(String customerId) {
		return sqlSessionTemplate.selectList("customer.cartShowDeal",customerId);
	}

	// 구매자 회원 정보 수정
	@Override
	public int updateCustomerInfo(CustomerVO customerVO){
		return sqlSessionTemplate.update("customer.customerupdate",customerVO);
	}
	
	// 장바구니 물품 삭제
	@Override
	public void deleteCart(CartVO cartVO) {
		sqlSessionTemplate.delete("customer.deleteCart",cartVO);		
	}
	
	// 장바구니 물품 중복여부 검사
	@Override
	public int searchCartItem(CartVO cartVO) {
		return sqlSessionTemplate.selectOne("customer.searchCartItem",cartVO);
	}

	// 장바구니 물품 중복시 수량 업데이트
	@Override
	public void updateCartItem(CartVO cartVO) {
		sqlSessionTemplate.update("customer.updateCartItem",cartVO);
	}
	
	// 물품 결제하기 
	@Override
	public TransactionVO transaction(TransactionVO transactionVO) {	
		sqlSessionTemplate.insert("customer.transactionAddItem",transactionVO);		
		int dual=sqlSessionTemplate.selectOne("customer.transactionDual");	
		
		StringTokenizer str = new StringTokenizer(transactionVO.getDealNumber(),",");
		StringTokenizer str2 = new StringTokenizer(transactionVO.getCartCount(),",");
		StringTokenizer str3 = new StringTokenizer(transactionVO.getTransactionPrice(),",");
		
		while(str.hasMoreTokens()){
			transactionVO.setDealNumber(str.nextToken());
			transactionVO.setCartCount(str2.nextToken());
			transactionVO.setTransactionPrice(str3.nextToken());
			
			sqlSessionTemplate.insert("customer.buyList",transactionVO);
			sqlSessionTemplate.update("customer.dealCountUpdate",transactionVO);
		}
		sqlSessionTemplate.delete("customer.transactionCartDelete",transactionVO.getCustomerId());	
		return sqlSessionTemplate.selectOne("customer.transactionResult",dual);
	}
	
	// 구매자 구매내역 보기
	@Override
	public List<BuyVO> buyCheck(String customerId) {
		return sqlSessionTemplate.selectList("customer.buyCheck",customerId);
	}	
	
	// 장바구니에 있는 물품 남은수량 비교하기
	@Override
	public int cartTranSactionCheck(String dealNumber) {
		return sqlSessionTemplate.selectOne("customer.cartTranSactionCheck",dealNumber);
	}
	
	// 장바구니 물품 초과시 삭제
	@Override
	public void cartTranSactionDel(String dealNumber,String customerId) {
		map.put("dealNumber", dealNumber);
		map.put("customerId", customerId);
		sqlSessionTemplate.delete("customer.cartTranSactionDel",map);
	}
	
	// 이메일 중복 체크 
	@Override
	public int emailCheck(String email) {
		return sqlSessionTemplate.selectOne("customer.emailCheck",email);
	}
	
	// 결제된 물품 뽑아오기
	public TransactionVO transactionResult(String transactionNumber ){
		return sqlSessionTemplate.selectOne("customer.transactionResult",transactionNumber);
	}
}
